//获取传递过来的值
var url = decodeURI(window.location.href);
var aa = url.indexOf('=');
if (aa > -1) {
    url = url.substring(aa + 1);
}

//购物车初始数据
function loadWebList() {
    var data = {
        "compID": url
    }
    $.ajax({
        url: "findShoppingByShoppingList",
        type: "POST",
        dataType: "json",
        data: JSON.stringify(data),
        contentType: 'application/json;charset=utf-8',
        success: function (data) {
            var code = ' ';
            if (data.code == 500) {
                Swal.fire("提示", "请添加购物车", "success");
            } else {
                for (var i = 0; i < data.shopping_List.length; i++) {
                    code += "  <input type=\"checkbox\" name=\"hobby\" value=" + data.shopping_List[i].shoppingID + "><div class=\"list-view-layout1\">\n" +
                        "                                <div class=\"product-box-layout3\">\n" +
                        "                                    <div class=\"item-img\">\n" +
                        "                                        <a href=web-single-product.html?id=" + data.shopping_List[i].recProductID + "><img style='width: 275px;height: 195px;' src='upload/" + data.shopping_List[i].recycleProductEntity.recProductImage + "'\n" +
                        "                                                                            alt=\"Product\"></a>\n" +
                        "                                    </div>\n" +
                        "                                    <div class=\"product-info\">\n" +
                        "                                        <div class=\"item-content\">\n" +
                        "                                            <h3 class=\"item-title\"><a href=web-single-product.html?id=" + data.shopping_List[i].recProductID + ">\n" +
                        "                                             " + data.shopping_List[i].recycleProductEntity.recProductName + "</a><span>New</span></h3>\n" +
                        "                                            <ul class=\"entry-meta\">\n" +
                        "                                                <li><i class=\"far fa-clock\"></i>   " + data.shopping_List[i].shoppingDate + "</li>\n" +
                        "                                            </ul>\n" +
                        "                                            <div class=\"btn-group\">\n" +
                        "                                                <a onclick='deleteShopping(" + data.shopping_List[i].shoppingID + ")'>Delete</a>\n" +
                        "                                            </div>\n" +
                        "                                        </div>\n" +
                        "                                        <div class=\"item-right\">\n" +
                        "                                            <div class=\"item-price\">\n" +
                        "                                                <span class=\"currency-symbol\">$</span>\n" +
                        "                                                    " + data.shopping_List[i].recycleProductEntity.recProductPrice + "<span>/</span>\n" +
                        "                               " + data.shopping_List[i].recycleProductEntity.recProductUnit + " \n" +
                        "                                            </div>\n" +
                        "                                            <div class=\"item-btn\">\n" +
                        "                                                <a href=web-single-product.html?id=" + data.shopping_List[i].recProductID + ">Details</a>\n" +
                        "                                            </div>\n" +
                        "                                        </div>\n" +
                        "                                    </div>\n" +
                        "                                </div>\n" +
                        "                            </div> ";
                }
                $("#data_1").html("");
                $("#data_2").html("");
                $("#data_1").append(code);
                $("#data_2").append("<div class=\"btn-prev disabled\">\n" +
                    "\n" +
                    "                            </div>\n" +
                    "                            <div class=\"page-number\">\n" +
                    "\n" +
                    "                            </div>\n" +
                    "                            <div class=\"btn-next\">\n" +
                    "                                <a onclick=\"orderInsert()\">下单<i class=\"fas fa-angle-double-right\"></i></a>\n" +
                    "                            </div>")
            }
        }
    });
}

//删除购物车
function deleteShopping(shoppingID) {
    var data = {
        "shoppingID": shoppingID
    };
    $.ajax({
        url: "updateShoppingByShoppingDeleteState",
        type: "POST",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(data),
        async: false,
        success: function (data) {
            if (data.code == 200) {
                Swal.fire("提示", "删除成功", "success");
                loadWebList();
            } else {
                Swal.fire("提示", "删除失败", "error");
            }
        }
    });

}

//下订单
function insertOrderByOrderData(recProductID) {
    var data = {
        "page": 1
    };
    $.ajax({
        url: "insertOrderByOrderData",
        type: "POST",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(data),
        async: false,
        success: function (data) {
            if (data.code == 200) {
                insertOrderProductByOrderProductData(recProductID);
            } else {
                Swal.fire("提示", "订单为未下成功", "error");
            }
        }
    });
}

//新增订单
function insertOrderProductByOrderProductData(recProductID) {
    var data = {
        "recProductID": recProductID
    };
    $.ajax({
        url: "insertOrderProductByOrderProductData",
        type: "POST",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(data),
        async: false,
        success: function (data) {
            if (data.code == 200) {

            } else {
                Swal.fire("提示", "订单为未下成功", "error");
            }
        }
    });
}

function orderInsert() {
    var myaaa = document.getElementsByName("hobby")
    var s = '';
    var flag = 0, pool = 1;
    for (var i = 0; i < myaaa.length; i++) {
        if (myaaa[i].checked) {
            flag = 1;
            if (pool == 1) {
                pool = 2;
                insertOrderByOrderData(myaaa[i].value);
            } else {
                insertOrderProductByOrderProductData(myaaa[i].value);
            }
        }
    }
    if (flag == 0) {
        Swal.fire("温馨提示", "请进行选择", "warning");
    }
    loadWebList();
}

$(function () {
    var DATA = '';
    $.ajax({
        url: "JudgeLogin_4",
        type: "POST",
        dataType: "json",
        contentType: 'application/json;charset=utf-8',
        success: function (data) {
            if (data.code == 500) {
                DATA += " <li class=\"header-btn\">\n" +
                    "                                    <a href=\"web-login.html\" class=\"item-btn\"><i class=\"fas fa-plus\"></i>登录</a>\n" +
                    "                                </li>"
                Swal.fire("温馨提示", "请先登录", "warning");
                window.location.href = "web-404.html";
            } else {
                DATA += " <li class=\"header-login-icon\">\n" +
                    "                                    <a href=\"web-account.html\" class=\"color-light\" data-toggle=\"tooltip\"\n" +
                    "                                       data-placement=\"top\" title=\"Login/Register\">\n" +
                    "                                        <i class=\"far fa-user\">我的</i>\n" +
                    "                                    </a>\n" +
                    "                                </li>\n" +
                    "                                <li class=\"header-btn\">\n" +
                    "                                    <a  onclick='tuichu()' class=\"item-btn\"><i class=\"fas fa-plus\"></i>退出</a>\n" +
                    "                                </li>";
                loadWebList();
            }
            $("#userData").html("");
            $("#userData").append(DATA);
        }
    });
})